#import <Foundation/Foundation.h>
#import <gtk/gtk.h>

typedef struct {
	GtkWidget *text;
	char *note;
}data_t,*pdata_t;

gboolean on_window_delete_event(GtkWidget *widget,GdkEvent *event,gpointer data){
	GtkWidget *text = ((pdata_t)data)->text;
	//GtkTextBuffer *buf = GTK_TEXT_VIEW(text)->buffer;
	GtkTextBuffer *buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
	NSLog(@"text at %p , buf at %p",text,buf);
	gint line_count,char_count;
	line_count = gtk_text_buffer_get_line_count(buf);
	char_count = gtk_text_buffer_get_char_count(buf);
	NSLog(@"line_count is %d , char_count is %d",line_count,char_count);
	GtkTextIter start,end;
	gtk_text_buffer_get_start_iter(buf,&start);
	gtk_text_buffer_get_end_iter(buf,&end);
	//NSLog(@"text:\n%s",buf->text);
	gchar *string = gtk_text_buffer_get_text(buf,&start,&end,TRUE);
	NSLog(@"text(%lu chars):\n%s",strlen(string),string);
	gtk_main_quit();
	return FALSE;
}

void my_main_quit(){
	//GtkTextBuffer *buffer = 
	gtk_main_quit();
}

int main(int argc,char **argv){
	GtkWidget *w,*text,*status_bar;

	gtk_init(&argc,&argv);

	w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_set_title(GTK_WINDOW(w),"Hello Editor");
	gtk_window_set_position(GTK_WINDOW(w),GTK_WIN_POS_CENTER);

	gtk_window_set_default_size(GTK_WINDOW(w),400,300);
	//GtkWidget *vbox = gtk_vbox_new(FALSE,0);
	GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL,0);
	gtk_container_add(GTK_CONTAINER(w),vbox);
	GtkWidget *scrolled_win = gtk_scrolled_window_new(NULL,NULL);
	text = gtk_text_view_new();
	gtk_box_pack_start(GTK_BOX(vbox),scrolled_win,TRUE,TRUE,0);
	gtk_container_add(GTK_CONTAINER(scrolled_win),text);
	gtk_text_view_set_editable(GTK_TEXT_VIEW(text),TRUE);

	status_bar = gtk_statusbar_new();
	gtk_box_pack_start(GTK_BOX(vbox),status_bar,FALSE,FALSE,0);

	//g_signal_connect(G_OBJECT(w),"destroy",G_CALLBACK(my_main_quit),NULL);
	data_t data = {.text=text,.note=NULL};
	g_signal_connect(G_OBJECT(w),"delete_event",G_CALLBACK(on_window_delete_event),
		&data);
	gtk_container_set_border_width(GTK_CONTAINER(w),2);
	gtk_widget_show_all(w);
	gtk_main();
	return 0;
}